package com.lst.projectlib.base;

import android.content.Context;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.FragmentActivity;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.ImageView;
import android.widget.PopupWindow;

import com.lst.projectlib.R;
import com.lst.projectlib.dialog.CustomProgressDialog;

public abstract class BaseActivity extends FragmentActivity {
	protected PopupWindow popup;
	protected CustomProgressDialog mDialog = null;
	protected Handler mHandler = new Handler(new Handler.Callback() {
		@Override
		public boolean handleMessage(Message msg) {
			refresh(msg.what, msg.obj);
			return false;
		}
	});

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		if (mDialog == null) {
			mDialog = new CustomProgressDialog(this, R.style.progress_dialog);
		}
		initView(savedInstanceState);
//		StatusBarCompat.translucentStatusBar(this);
		initData();
	}

	protected abstract void initView(Bundle savedInstanceState);
	protected abstract void initData();

	protected void runLoadThread(final int messageId, final Object params) {
		new Thread(new Runnable() {
			@Override
			public void run() {
				Message msg = new Message();
				msg.what = messageId;
				// Load data for the activity.
				msg.obj = loadData(messageId, params);
				mHandler.sendMessage(msg);
			}
		}).start();
	}
	
	protected void refresh(int messageId, Object params) {}
	protected Object loadData(int messageId, Object params) {
		return null;
	}

	/**
	 * 关闭页面方法
	 */
	protected void back() {
		hideKeyboard();
		finish();
	}

	//隐藏软键盘
	protected void hideKeyboard() {
		//判断隐藏软键盘是否弹出
		if (getWindow().getAttributes().softInputMode == WindowManager.LayoutParams.SOFT_INPUT_STATE_UNSPECIFIED) {//这个判断并不起作用
			if (getCurrentFocus() != null) {
				InputMethodManager manager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
				manager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
			}
		}
	}

	//隐藏键盘
	protected boolean hideKeyboard(View focusView, View reFocusView){
		InputMethodManager inputMethodManager = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
		if(inputMethodManager.isActive(focusView)){
			reFocusView.requestFocus();//使其它view获取焦点
			inputMethodManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
			return true;
		}
		return false;
	}

	@Override
	protected void onDestroy() {
		if (mDialog != null && mDialog.isShowing()) {
			mDialog.dismiss();
		}
		super.onDestroy();
	}

	@Override
	public void onBackPressed() {
		if (popup != null && popup.isShowing()) {
			dismissPopWindow();
		} else {
			super.onBackPressed();
		}
	}
	
	@Override
	public Resources getResources() {
		Resources res = super.getResources();    
	    Configuration config=new Configuration();    
	    config.setToDefaults();    
	    res.updateConfiguration(config,res.getDisplayMetrics() );  
	    return res;  
	}

	protected void dismissPopWindow() {
		if (popup != null && popup.isShowing()) {
			popup.dismiss();
		}
	}

	protected void showPopupWindow(View view){
		WindowManager.LayoutParams params = getWindow().getAttributes();
		params.alpha = 0.7f;
		getWindow().setAttributes(params);
		if (popup == null)
			popup = new PopupWindow(this);
		ImageView imageView = new ImageView(this);
		imageView.setImageDrawable(getResources().getDrawable(R.drawable.ifast));
		popup.setContentView(imageView);
		popup.setOutsideTouchable(true);
		popup.setFocusable(true);
		popup.setTouchable(true);
		imageView.setOnTouchListener(new View.OnTouchListener() {
			@Override
			public boolean onTouch(View view, MotionEvent motionEvent) {
				dismissPopWindow();
				return false;
			}
		});
		DisplayMetrics metric = new DisplayMetrics();
		getWindowManager().getDefaultDisplay().getMetrics(metric);
		int width = metric.widthPixels; // 屏幕宽度（像素）
		int height = metric.heightPixels; // 屏幕高度（像素）
		popup.setWidth(width);
		popup.setHeight(height / 4 * 3);
		popup.setBackgroundDrawable(getResources().getDrawable(R.color.transparency));
		popup.showAtLocation(view, Gravity.CENTER, 0, 0);
		popup.setOnDismissListener(new PopupWindow.OnDismissListener() {
			@Override
			public void onDismiss() {
				WindowManager.LayoutParams params = getWindow().getAttributes();
				params.alpha = 1.0f;
				getWindow().setAttributes(params);
			}
		});
	}
}
